package org.dromara.system.controller.system;

import cn.dev33.satoken.annotation.SaIgnore;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.RenewSimBo;
import org.dromara.system.domain.bo.SimRenewRecordsBo;
import org.dromara.system.domain.vo.SimRenewRecordsVo;
import org.dromara.system.service.ISimRenewRecordsService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 续期记录
 *
 * @author Lion Li
 * @date 2025-08-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/renewRecords")
public class SimRenewRecordsController extends BaseController {

    private final ISimRenewRecordsService simRenewRecordsService;

    /**
     * 查询续期记录列表
     */
//    @SaCheckPermission("system:renewRecords:list")
    @GetMapping("/list")
    public TableDataInfo<SimRenewRecordsVo> list(SimRenewRecordsBo bo, PageQuery pageQuery) {
        return simRenewRecordsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出续期记录列表
     */
//    @SaCheckPermission("system:renewRecords:export")
    @Log(title = "续期记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SimRenewRecordsBo bo, HttpServletResponse response) {
        List<SimRenewRecordsVo> list = simRenewRecordsService.queryList(bo);
        ExcelUtil.exportExcel(list, "续期记录", SimRenewRecordsVo.class, response);
    }

    /**
     * 获取续期记录详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("system:renewRecords:query")
    @GetMapping("/{id}")
    public R<SimRenewRecordsVo> getInfo(@NotNull(message = "主键不能为空")
                                        @PathVariable Long id) {
        return R.ok(simRenewRecordsService.queryById(id));
    }

    /**
     * 新增续期记录
     */
//    @SaCheckPermission("system:renewRecords:add")
    @Log(title = "续期记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SimRenewRecordsBo bo) {
        return toAjax(simRenewRecordsService.insertByBo(bo));
    }

    /**
     * 修改续期记录
     */
//    @SaCheckPermission("system:renewRecords:edit")
    @Log(title = "续期记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SimRenewRecordsBo bo) {
        return toAjax(simRenewRecordsService.updateByBo(bo));
    }

    /**
     * 删除续期记录
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("system:renewRecords:remove")
    @Log(title = "续期记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(simRenewRecordsService.deleteWithValidByIds(List.of(ids), true));
    }


    @PostMapping("/renewSim")
    public R<String> renewSim(@RequestBody RenewSimBo bo) {
        return R.ok("支付拉起成功！", simRenewRecordsService.renewSim(bo));
    }

    @PostMapping("/renewSimWxCallBack")
    @SaIgnore
    public String renewSimWxCallBack(HttpServletRequest request, HttpServletResponse response) {
        return simRenewRecordsService.renewSimWxCallBack(request, response);
    }


}
